package com.lzh.dlykserver.controller;

import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.entity.TDicValue;
import com.lzh.dlykserver.query.DicValueDTO;
import com.lzh.dlykserver.result.R;
import com.lzh.dlykserver.service.DicValueService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class DicValueController {

    private final DicValueService dicValueService;

    /**
     * 获取字典值
     * @param current
     * @param dicValueDTO
     * @return
     */
    @GetMapping("/dict-values")
    public R getDicValues(@RequestParam(value = "current", required = false) Integer current,
                          DicValueDTO dicValueDTO) {
        if (current == null) {
            current = 1;

        }
        PageInfo<TDicValue> pageInfo = dicValueService.getDicValues(current, dicValueDTO);
        return R.OK(pageInfo);
    }

    /**
     * 获取字典值
     * @param id
     * @return
     */
    @GetMapping("/dict-value/{id}")
    public R getDicValueById(@PathVariable(value = "id") Integer id) {
        TDicValue dicValue = dicValueService.getDicValueById(id);
        return R.OK(dicValue);
    }

    /**
     * 删除字典值
     * @param id
     * @return
     */
    @DeleteMapping("/dict-value/{id}")
    public R deleteDicValueById(@PathVariable(value = "id") Integer id) {
        int i = dicValueService.deleteDicValueById(id);
        return R.OK(i);
    }

    /**
     * 批量删除字典值
     * @param ids
     * @return
     */
    @DeleteMapping("/dict-values")
    public R deleteDicValueByIds(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        int i = dicValueService.deleteDicValueByIds(idList);
        return i > 0 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑字典值
     * @param dicValue
     * @return
     */
    @PutMapping("/dict-value/edit")
    public R editDicValue(@RequestBody TDicValue dicValue) {
        int i = dicValueService.editDicValue(dicValue);
        return i > 0 ? R.OK() : R.FAIL();
    }

    /**
     * 添加字典值
     * @param dicValue
     * @return
     */
    @PostMapping("/dict-value/add")
    public R addDicValue(@RequestBody TDicValue dicValue) {
        int i = dicValueService.addDicValue(dicValue);
        return i > 0 ? R.OK() : R.FAIL();
    }

}
